package admin

import (
	"context"

	"server/internal/svc"
	"server/internal/types"
	"server/models"
	"server/utils"

	"github.com/jinzhu/copier"
	"github.com/zeromicro/go-zero/core/logx"
)

type CategoryListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCategoryListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CategoryListLogic {
	return &CategoryListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CategoryListLogic) CategoryList(req *types.CategoryListReq) (resp []types.CategoryListResp, err error) {
	datas := []models.Category{}
	tx := utils.Db.Order("level asc").Model(&models.Category{})
	utils.TxFind(tx, &datas)
	for _, item := range datas {
		respItem := types.CategoryListResp{}
		copier.Copy(&respItem, item)
		if item.ParentID != 0 {
			respItem.Name = "├─" + respItem.Name
		}
		resp = append(resp, respItem)
	}

	return resp, nil
}
